package com.forever.jodd;

import java.io.File;
import java.io.IOException;

import jodd.io.FileUtil;
import jodd.mail.Email;
import jodd.mail.EmailAttachment;
import jodd.mail.EmailMessage;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpSslServer;
import jodd.mail.att.ByteArrayAttachment;
import jodd.mail.att.FileAttachment;
import jodd.util.MimeTypes;

import org.junit.Test;

public class EmailUtil {
	public static void main(String[] args) {
		Email email = Email.create();
		email.addMessage(new EmailMessage("消息", "消息"));
		email.addText("邮件内容");
		email.embed(EmailAttachment.attachment().file(new File("d:/console.txt")).create());
		email.from("771842634@qq.com").to("771842634@qq.com");
		email.subject("主题");
		SendMailSession mailSession = new SmtpSslServer("smtp.qq.com//发送端邮箱服务器协议", "发送端QQ邮箱", "发送端QQ邮箱密码").createSession();
		mailSession.open();
		mailSession.sendMail(email);
		mailSession.close();
		System.out.println("发送成功!...");
	}

	@Test
	public void test() throws IOException {
		Email email = new Email();

		email.setFrom("infoxxx@jodd.org");
		email.setTo("igorxxxx@gmail.com");
		email.setSubject("test7");

		EmailMessage textMessage = new EmailMessage("Hello!", MimeTypes.MIME_TEXT_PLAIN);
		email.addMessage(textMessage);

		EmailMessage htmlMessage = new EmailMessage("<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">"
				+ "<body><h1>Hey!</h1><img src='cid:c.png'><h2>Hay!</h2></body></html>", MimeTypes.MIME_TEXT_HTML);
		email.addMessage(htmlMessage);

		EmailAttachment embeddedAttachment = new ByteArrayAttachment(FileUtil.readBytes("d:\\c.png"), "image/png", "c.png", "c.png");
		email.attach(embeddedAttachment);

		EmailAttachment attachment = new FileAttachment(new File("d:\\b.jpg"), "b.jpg", "image/jpeg");
		email.attach(attachment);
	}

}